<template>
  <div>
    <el-form ref="myform" :model="formItem" label-width="100px" size="mini" class="form-view">
      <el-row :gutter="40">
        <el-col :span="8">
          <el-form-item label="工单号">
            {{ formItem.workOrderNum }}
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="标题">
            {{ formItem.name }}
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="配置项">
            {{ formItem.objName }}
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="8">
          <el-form-item label="事件来源">
            {{ formItem.incidentFromName }}
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="登记人">
            {{ formItem.registerUserName }}
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="登记时间">
            {{ formItem.createTimeShow }}
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="8">
          <el-form-item label="申请人">
            {{ formItem.applyUserName }}
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="申请方式">
            {{ formItem.applyTypeName + ":" + formItem.communicationNumber }}
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="申请时间">
            {{ formItem.applyTimeShow }}
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="8">
          <el-form-item label="影响范围">
            {{ formItem.coverageInfluenceName }}
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="紧急程度">
            {{ formItem.urgencyName }}
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="优先级" prop="priority">
            {{ formItem.priorityName }}
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item label="详细说明">
            <div class="richWraper" v-html="formItem.description" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item label="附件">
            <el-button v-for="tag in formItem.attachments" :key="tag.name" size="mini" type="text" @click.native="downloadFile(tag)">{{ tag.name }}</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { detail } from '@neu/dcom-ui/api/incidentRegister'
import { downloadFile } from '@neu/dcom-ui/api/incidentRegister'
import { getToken } from '@neu/biz-core/utils/auth'
import { formatDate } from '@neu/biz-core/utils/utils'
const formItem = {
}
export default {
  name: 'IncidentRegisterView',
  components: {
  },
  props: {
    taskId: {
      required: false,
      type: String,
      default: 'NULL'
    },
    workOrderNum: {
      required: false,
      type: String,
      default: ''
    }
  },
  data() {
    return {
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Authentication': getToken()
      },
      // 编辑的表单内容
      formItem: {
        ...formItem
      }
    }
  },
  created() {
    this.fetch()
  },
  methods: {
    fetch() {
      if (this.workOrderNum !== 'NULL') {
        detail(this.workOrderNum, this.taskId).then(response => {
          this.formItem = {
            ...response.data
          }
          this.formItem.applyTimeShow = formatDate(this.formItem.applyTime)
          this.formItem.createTimeShow = formatDate(formItem.createTime)
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    /** 下载附件 */
    downloadFile(tag) {
      downloadFile(tag.fsId).then(res => {
        const content = res
        const blob = new Blob([content])
        const fileName = tag.name
        if ('download' in document.createElement('a')) { // 非IE下载
          const elink = document.createElement('a')
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href) // 释放URL 对象
          document.body.removeChild(elink)
        } else { // IE10+下载
          navigator.msSaveBlob(blob, fileName)
        }
      })
    }
  }
}
</script>
